<?php

/**
* HuntCodeTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';

class  HuntCodeTest extends WebTestCase
{

    private $testCodeName,$testSlideName,$testDesc,$testCodeId,$testSlideId,$url,$code,$dbh;
    private $adminUsername,$adminPassword;
    private $username = '';
    private $password = '';

    private $username1 = '';
    private $password1 = '';


    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $this->testDesc = md5( uniqid( rand(), true ) );
        
        $this->username1 = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password1 = "zuza";


        $this->adminUsername = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->adminPassword = "haslo";

        $this->code = substr(md5(uniqid(rand(), true)),0,6);

        $this->testCodeName = md5(uniqid(rand(), true));
        $this->testSlideName = md5(uniqid(rand(), true));

        $locale =  &PEAR::getStaticProperty('CONFIG_INI','LOCALE');
        $this->locale = $locale['default_locale']; 
        
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];

        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $this->dbh = new PDO($options['dsn_string']); 
        $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        
        AuthSingleton::instance()->addUser($this->adminUsername,$this->adminPassword);
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413");

    }
        
    public function testUserAuthCodeOk()
    {
        $this->get($this->url.'?');
        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        
        $this->dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->username."')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");


        $this->testCodeId =  $lastInsertId;
        
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $this->dbh->exec("UPDATE guide SET  pic_file = '".$this->testCodeId."image.jpg' WHERE rowid=".$this->testCodeId);
        copy( dirname(__FILE__).'/image.jpg', $upload['upload_dir'].$this->testCodeId.'image.jpg');
        

        $this->get($this->url.'?c='.$this->code);
        
        $this->assertText('Celny strzał. Kolejne trofeum do kolekcji!');
        $this->assertText($this->testCodeName);
        $this->assertText($this->testDesc);
        $this->assertPattern('/send_image.php\?id='.$this->testCodeId.'image.jpg/');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
        
    }

    public function testUserAuthCodeOkRemoveCode()
    {
        $this->get($this->url.'?');
        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        
        $this->dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->adminUsername."')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");


        $this->testCodeId =  $lastInsertId;
        
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $this->dbh->exec("UPDATE guide SET  pic_file = '".$this->testCodeId."image.jpg' WHERE rowid=".$this->testCodeId);
        copy( dirname(__FILE__).'/image.jpg', $upload['upload_dir'].$this->testCodeId.'image.jpg');
        

        $this->get($this->url.'?c='.$this->code);
        
        $this->assertText('Celny strzał. Kolejne trofeum do kolekcji!');
        $this->assertText($this->testCodeName);
        $this->assertText($this->testDesc);
        $this->assertPattern('/send_image.php\?id='.$this->testCodeId.'image.jpg/');       

        $this->get($this->url.'?cmd=AdminCodeList');
        $this->assertTitle('Polowanie na kody Administracja');
        $this->setFieldByName('username', $this->adminUsername);
        $this->setFieldByName('password', $this->adminPassword);
        $this->click("Login");
        
        $this->clickLinkById('AdminRemoveCode'.$lastInsertId);
        $this->assertText('Usunąć kod "'.$this->testCodeName.'" ?');
        $this->clickSubmit('Tak');
        $this->assertText('Kod usunięty.');        
        
        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
        
    }
    

    public function testUserAuthCodeOkNotPublished()
    {
        $this->get($this->url.'?');
        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        
        $this->dbh->exec("INSERT INTO guide ( code, owner,publish ) VALUES ('".$this->code."','".$this->username."','')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");

        $this->testCodeId =  $this->dbh->lastInsertId();
        
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $this->dbh->exec("UPDATE guide SET  pic_file = '".$this->testCodeId."image.jpg' WHERE rowid=".$this->testCodeId);
        copy( dirname(__FILE__).'/image.jpg', $upload['upload_dir'].$this->testCodeId.'image.jpg');
        

        $this->get($this->url.'?c='.$this->code);
        
        $this->assertText('Kod nie jest poprawny. Spróbuj jeszcze raz');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
        
    }
    
    public function testUserAuthCodeOkTwoUsers()
    {
        $this->get($this->url.'?');
        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        AuthSingleton::instance('AUTH_USER')->addUser($this->username1,$this->password1);

        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        
        $this->dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->username."')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $this->testCodeId =  $lastInsertId;

        $this->get($this->url.'?c='.$this->code);
        
        $this->assertText('Celny strzał. Kolejne trofeum do kolekcji!');

        $this->assertText($this->testDesc);
        $this->assertNoPattern('/send_image.php\?id='.$this->testCodeId.'image.jpg/');
        $this->get($this->url.'?cmd=HuntCode&logout=1');

        //Second user

        $this->get($this->url);


        $this->setFieldByName('username', $this->username1);
        $this->setFieldByName('password', $this->password1);
        $this->click("Login");

        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');

        $this->get($this->url.'?c='.$this->code);
              
        $this->assertText('Celny strzał. Kolejne trofeum do kolekcji!');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }


    public function testUserAuthCodeOkTwoTimes()
    {
        $this->get($this->url.'?');
        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        
        $this->dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->username."')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $this->testCodeId =  $lastInsertId;

        $this->get($this->url.'?c='.$this->code);
        
        $this->assertText('Celny strzał. Kolejne trofeum do kolekcji!');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);

        $this->get($this->url.'?c='.$this->code);
        $this->assertText('Kod był już upolowany.');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }

    public function testUserAuthWrongCode()
    {
        $this->get($this->url.'?');
        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');
        $this->assertText('Mapa poglądowa oraz lista trofeów');
        
        $this->dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->adminUsername."')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $this->testCodeId =  $lastInsertId;

        $this->get($this->url.'?c=123456');
        
        $this->assertText('Kod nie jest poprawny. Spróbuj jeszcze raz');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);


    }

    public function testUserNotAuthUserAuthCodeOk()
    {
        
        $this->dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->adminUsername."')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $this->testCodeId =  $lastInsertId;

        $this->get($this->url.'?c='.$this->code);

        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');    
   
        $this->assertText('Celny strzał. Kolejne trofeum do kolekcji!');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }


    public function testUserNotAuthUserAuthWrongCode()
    {
        
        $this->dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->adminUsername."')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $this->testCodeId =  $lastInsertId;

        $this->get($this->url.'?c=888888');

        AuthSingleton::instance('AUTH_USER')->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');    
   
        $this->assertText('Kod nie jest poprawny. Spróbuj jeszcze raz');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],0);
    }

    public function testUserNotAuthAddUserAuthUserCodeOk()
    {
        
        $this->dbh->exec("INSERT INTO guide ( code, owner ) VALUES ('".$this->code."','".$this->adminUsername."')");
        $lastInsertId = $this->dbh->lastInsertId();
        $this->dbh->exec("INSERT INTO guide_name ( guide_id,name,locale ) VALUES ('".$lastInsertId."','".$this->testCodeName."','".$this->locale."')");
        $this->dbh->exec("INSERT INTO guide_desc ( guide_id,desc,locale ) VALUES ('".$lastInsertId."','".$this->testDesc."','".$this->locale."')");
        $this->testCodeId =  $lastInsertId;

        $this->get($this->url.'?c='.$this->code);

        $this->click('Nowy użytkownik');

        $this->assertText('Podaj login i hasło. Użyj działającego adresu e-mail jako login.');
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->setFieldByName('passwordRepeat', $this->password);
        $this->click("Dodaj");
        $this->assertText('Użytkownik dodany.');

        $this->click('Zaloguj się do serwisu.');

        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");
        $this->assertTitle('Polowanie na kody');    
   
        $this->assertText('Celny strzał. Kolejne trofeum do kolekcji!');

        $sth = $this->dbh->prepare('SELECT COUNT(ROWID) FROM hunted_code WHERE code_id = :code_id AND owner = :owner AND time IS NOT NULL');
        $sth -> bindParam(':code_id',  $this->testCodeId ,PDO::PARAM_INT);
        $sth -> bindParam(':owner', $this->username ,PDO::PARAM_STR);
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);
        $this->assertEqual($result[0],1);
    }
    
    public function tearDown() 
    {
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD'); 
          
        $sth = $this->dbh->query("SELECT pic_file,rowid FROM guide 
                                  WHERE owner = \"".$this->username."\" 
                                  OR owner = \"".$this->username1."\"
                                  OR owner = \"".$this->adminUsername."\"");
        
        for($i=0; $result = $sth->fetch(); $i++){

            
            $this->dbh->exec("DELETE FROM guide_name WHERE guide_id='".$result['rowid']."'");
            $this->dbh->exec("DELETE FROM guide_desc WHERE guide_id='".$result['rowid']."'");

            if ( is_file($upload['upload_dir'].$result['pic_file']) )
            {
                unlink($upload['upload_dir'].$result['pic_file']);
            }
        }



        $this->dbh->exec("DELETE FROM guide WHERE owner='".$this->username."'");
        $this->dbh->exec("DELETE FROM guide WHERE owner='".$this->username1."'");
        $this->dbh->exec("DELETE FROM guide WHERE owner='".$this->adminUsername."'");
        $this->dbh->exec("DELETE FROM hunted_code WHERE owner='".$this->username."'");
        $this->dbh->exec("DELETE FROM hunted_code WHERE owner='".$this->username1."'");
        $dbh = null;

        $this->get($this->url.'?cmd=AdminCodeList&logout=1');
        AuthSingleton::instance('AUTH_USER')->removeUser($this->username1);
        AuthSingleton::instance('AUTH_USER')->removeUser($this->username);



        AuthSingleton::instance()->removeUser($this->adminUsername);
        
    }
 

}
?>
